﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Royaumes_Libres.Database;
using System.Data.SqlClient;
using System.Data;

namespace Royaumes_Libres
{
    public partial class usermap : System.Web.UI.Page
    {
        cUsers pUser;
        cMapKingdom[] allKingdom;
        cKingdom pKingdom;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Properties.Settings.Default.ObjUser] != null)
            {
                pUser = (cUsers)Session[Properties.Settings.Default.ObjUser];
                pKingdom = new cKingdom(pUser.PkUser, pUser.ActiveWorld);
                allKingdom = cMapKingdom.GetAllKingdom(pUser.ActiveWorld);
                GetMap();
            }
        }

        protected void GetMap() 
        {
            int ic = 1;
            int ir = 1;
            TableRow myrow = new TableRow();
            this.tblMap.Controls.Add(myrow);

            while (ir <= 10)
            {
                while (ic <= 20)
                {
                    TableCell mycell = new TableCell();
                    myrow.Controls.Add(mycell);
                    ++ic;
                }
                myrow = new TableRow();
                this.tblMap.Controls.Add(myrow);
                ic = 1;
                ++ir;
            }
            GetPosition();
        }

        protected void GetPosition()
        {
            foreach (cMapKingdom kingdom in allKingdom)
            {
                ucKingdomMap uc = kingdom.GetControl(Page);
                uc.mapKingdom = kingdom;
                this.tblMap.Rows[kingdom.pPosY-1].Cells[kingdom.pPosX-1].Controls.Add(uc);

                if (kingdom.pPosX - 1 == pKingdom.PosX - 1 && kingdom.pPosX - 1 == pKingdom.PosX - 1)
                {
                    uc.isMine = true;
                }
            }

            this.tblMap.Rows[pKingdom.PosY - 1].Cells[pKingdom.PosX - 1].Style.Add("border", "1px double gold");
        }
    }
}